package nickthissen.iracing.common;

import android.content.Context;
import android.util.Log;

import nickthissen.iracing.datamodels.Result;
import nickthissen.iracing.datamodels.User;
import nickthissen.iracing.networking.Networking;

/**
 * Created by nthissen on 24/05/13.
 */
public class ServiceLogging
{
    public static void logLogin(Context context, User user)
    {
        Task<User> task = new Task<User>(context,
                new DataLoader<User>()
                {
                    @Override
                    public Result getData(User in)
                    {
                        return Networking.logLogin(this.getContext(), in);
                    }

                    @Override
                    public void onFinished(Result out)
                    {
                        if (out == null) out = Result.failed();
                        Log.i("AutoLog", "Login logged to service: " + out.Success);
                    }
                });
        task.execute(user);
    }

    public static void logPageRequest(Context context, String url)
    {
        Task<String> task = new Task<String>(context,
                new DataLoader<String>()
                {
                    @Override
                    public Result getData(String in)
                    {
                        return Networking.logPageRequest(this.getContext(), in);
                    }

                    @Override
                    public void onFinished(Result out)
                    {
                        if (out == null) out = Result.failed();
                        Log.i("AutoLog", "Request logged to service: " + out.Success);
                    }
                });
        task.execute(url);
    }

    public static void logError(Context context, Error error)
    {
        Task<Error> task = new Task<Error>(context,
                new DataLoader<Error>()
                {
                    @Override
                    public Result getData(Error in)
                    {
                        return Networking.logError(this.getContext(), in);
                    }

                    @Override
                    public void onFinished(Result out)
                    {
                        if (out == null) out = Result.failed();
                        Log.i("AutoLog", "Error logged to service: " + out.Success);
                    }
                });
        task.execute(error);
    }
}
